package com.naihe.anderson.sms.xml;

import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Xml;

import com.naihe.anderson.sms.util.FileUtil;

public class IncomeXmlUtil {

	public static final String filePath = "income.xml";
	
	public List<String> getIncomeByXml(Context context){
		File f = context.getFileStreamPath(filePath);  
        if(f.exists())
        {  
            SAXParserFactory factory = SAXParserFactory.newInstance();  
            try {  
                SAXParser sp = factory.newSAXParser();  
                IncomeXmlHandler handler = new IncomeXmlHandler();  
                sp.parse(f, handler);
                
                return handler.getProjects();
            } catch(Exception e) {  
            	FileUtil.addLog("IncomeXmlUtil::getIncomeByXml", IncomeXmlUtil.class, "IncomeXmlUtil", e);
            }  
        }  
        
        return new ArrayList<String>();
	}
	
	public void saveExpenditure(Context context, List<String> expenditureList){
		if(expenditureList == null)
			return;
		
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try{
			serializer.setOutput(writer);
			// <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
			serializer.startDocument("UTF-8",true);
		
			// <SignatureMsgss>
			serializer.startTag("","Income");

			for(String project: expenditureList){
				// <project>qingzhi001</project>
				serializer.startTag("", "project");
				serializer.text(project);
				serializer.endTag("", "project");
			}
			
			// </SignatureMsgList>
			serializer.endTag("", "Income");
			serializer.endDocument();
			write(writer.toString(), context);
		}catch (Exception e) {
			
		}
	}
	
	private boolean write(String txt,Context context)
	{
		try
		{
			OutputStream os = context.openFileOutput(filePath, Context.MODE_PRIVATE);
			OutputStreamWriter osw=new OutputStreamWriter(os);
			osw.write(txt);
			osw.close();
			os.close();
		}
		catch(Exception e)
		{
			return false;
		}
		return true;
	}
	
	private class IncomeXmlHandler extends DefaultHandler {

		List<String> projects;
		private String tempString;
		
		@Override
		public void startDocument() throws SAXException {
			projects = new ArrayList<String>();
		}
		
		public List<String> getProjects(){
			if(projects == null)
				projects = new ArrayList<String>();
			return projects;
		}
		
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
	        tempString = localName;
		}
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
            String valueString = new String(ch, start, length);  
            if ("project".equals(tempString)) {  
                // 如果当前解析到的节点是name 就要将name中的文本节点元素的值得到  
            	projects.add(valueString);
            }
		}
		
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
	        tempString = null;
		}

		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}
	}
}
